package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zmj.sy.mom.srv.aps.bean.dto.GroupUser.GroupUserDto;
import com.zmj.sy.mom.srv.aps.bean.dto.staff.StaffDto;
import com.zmj.sy.mom.srv.aps.bean.dto.staff.TeamSelectDto;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Group;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.GroupUser;
import com.zmj.sy.mom.srv.aps.bean.vo.group.*;
import com.zmj.sy.mom.srv.aps.feign.StaffApiClient;
import com.zmj.sy.mom.srv.aps.mapper.GroupMapper;
import com.zmj.sy.mom.srv.aps.mapper.GroupUserMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class GroupUserService {

    private final GroupUserMapper groupUserMapper;
    private final GroupMapper groupMapper;
    private final StaffApiClient staffApiClient;

    /**
     * @Description:  添加组
     * @author: lyh
     * @Param:
     * @date: 2024/9/20 9:14
     */
    @Transactional
    public void addGroupUser(GroupAddReqVo vo){
        LambdaQueryWrapper<Group> groupWrapper = new LambdaQueryWrapper<>();
        groupWrapper.eq(Group::getDeleted,0);
        groupWrapper.eq(Group::getMyCode,vo.getMyCode());
        Group group = groupMapper.selectOne(groupWrapper);

        //人员为空
        LambdaQueryWrapper<GroupUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GroupUser::getDeleted, 0);
        lambdaQueryWrapper.eq(GroupUser::getGroupId, vo.getMyCode());
        List<GroupUser> oldList = groupUserMapper.selectList(lambdaQueryWrapper);
        if(ObjectUtils.isEmpty(oldList)){
            //人员为空
            if(ObjectUtils.isEmpty(vo.getStaffList())){
                GroupUser newOne = new GroupUser();
                newOne.setGroupId(group.getId());
                newOne.setDeleted(0);
                groupUserMapper.insert(newOne);
            }else {
                //人员不为空
                for(StaffDto staffDto : vo.getStaffList()){
                    GroupUser groupUser = new GroupUser();
                    groupUser.setUserCode(staffDto.getStaffCode());
                    groupUser.setUserName(staffDto.getStaffName());
                    groupUser.setGroupId(group.getId());
                    groupUser.setDeleted(0);

                    LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(GroupUser::getGroupId, groupUser.getGroupId());
                    queryWrapper.eq(GroupUser::getUserCode, groupUser.getUserCode());
                    queryWrapper.eq(GroupUser::getDeleted, 0);
                    GroupUser oldUser = groupUserMapper.selectOne(queryWrapper);
                    if(ObjectUtils.isEmpty(oldUser)){
                        //新增操作
                        groupUserMapper.insert(groupUser);
                    }
                }
            }
        }
    }

    /**
     * @Description:  查看员工-组
     * @author: lyh
     * @Param:
     * @date: 2024/9/20 9:16
     */
    public List<GroupUserDto> getGroupUser(Integer groupId){
        LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupUser::getDeleted, 0);
        if(ObjectUtils.isNotEmpty(groupId)){
            queryWrapper.eq(GroupUser::getGroupId, groupId);
        }
        List<GroupUser> list = groupUserMapper.selectList(queryWrapper);

        List<GroupUserDto> resultList = new ArrayList<>();
        Map<Integer, Group> mapGroup = groupMapper.lambdaQuery()
                .eq(Group::getDeleted, 0).list()
                .stream().collect(Collectors.toMap(Group::getId, Function.identity()));
        for(GroupUser user : list){
            GroupUserDto groupUserDto = new GroupUserDto();
            groupUserDto.setGroupId(user.getGroupId());
            groupUserDto.setStaffCode(user.getUserCode());
            groupUserDto.setStaffName(user.getUserName());
            if(ObjectUtils.isNotEmpty(mapGroup.get(user.getGroupId()))){
                groupUserDto.setTeamId(mapGroup.get(user.getGroupId()).getTeamId());
                groupUserDto.setTeamName(mapGroup.get(user.getGroupId()).getTeamName());
            }
            resultList.add(groupUserDto);
        }
        return resultList;
    }

    /**
     * @Description:  人员查询列表
     * @author: lyh
     * @Param:
     * @date: 2024/9/20 9:05
     */
    public List<StaffDto> staffDtoList(StaffDto staffDto){
        List<StaffDto> staffDtoList = staffApiClient.staffList(staffDto).getData();
        return staffDtoList;
    }


    /**
     * @Description:  班组查询
     * @author: lyh
     * @Param:
     * @date: 2024/10/16 18:06
     */
    public List<TeamSelectDto> getTeamList(TeamSelectDto teamDto){
        List<TeamSelectDto> dtoList = staffApiClient.teamDtoList(teamDto).getData();
        return dtoList;
    }

    public List<String> getUserTile(String userCode) {
        LambdaQueryWrapper<GroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupUser::getDeleted, 0);
        queryWrapper.eq(GroupUser::getUserCode, userCode);
        List<GroupUser> list = groupUserMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Group> group = groupMapper.lambdaQuery()
                    .eq(Group::getDeleted, 0)
                    .in(Group::getId, list.stream().map(GroupUser::getGroupId).distinct().collect(Collectors.toList())).list();
            if(!CollectionUtils.isEmpty(group)){
                return group.stream().map(Group::getWorkshopTaskTitleVal).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
            }
        }
        return new ArrayList<>();
    }
}
